# ui/login_window.py

from PyQt6.QtWidgets import (QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout)
from PyQt6.QtCore import pyqtSignal
from utils.communication import send_request
from ui.dialogs import show_info, show_error

class LoginWindow(QWidget):
    # 登录成功信号，传出用户名和是否管理员标志
    login_success = pyqtSignal(str, bool)

    def __init__(self):
        super().__init__()
        self.setWindowTitle("自助订餐系统 - 登录/注册")
        self.resize(300, 200)

        self.is_login_mode = True  # True登录 False注册

        # 创建控件
        self.label_title = QLabel("登录")
        self.label_username = QLabel("用户名:")
        self.edit_username = QLineEdit()
        self.label_password = QLabel("密码:")
        self.edit_password = QLineEdit()
        self.edit_password.setEchoMode(QLineEdit.EchoMode.Password)

        self.btn_submit = QPushButton("登录")
        self.btn_switch = QPushButton("切换到注册")

        # 布局
        layout = QVBoxLayout()
        layout.addWidget(self.label_title)

        form_layout = QVBoxLayout()
        form_layout.addWidget(self.label_username)
        form_layout.addWidget(self.edit_username)
        form_layout.addWidget(self.label_password)
        form_layout.addWidget(self.edit_password)

        layout.addLayout(form_layout)
        layout.addWidget(self.btn_submit)
        layout.addWidget(self.btn_switch)

        self.setLayout(layout)

        # 绑定事件
        self.btn_submit.clicked.connect(self.on_submit)
        self.btn_switch.clicked.connect(self.on_switch_mode)

    def on_switch_mode(self):
        # 切换登录/注册模式
        if self.is_login_mode:
            self.is_login_mode = False
            self.label_title.setText("注册")
            self.btn_submit.setText("注册")
            self.btn_switch.setText("切换到登录")
        else:
            self.is_login_mode = True
            self.label_title.setText("登录")
            self.btn_submit.setText("登录")
            self.btn_switch.setText("切换到注册")

    def on_submit(self):
        username = self.edit_username.text().strip()
        password = self.edit_password.text().strip()

        if not username or not password:
            show_error(self, "错误", "用户名和密码不能为空")
            return

        if self.is_login_mode:
            self.login(username, password)
        else:
            self.register(username, password)

    def login(self, username, password):
        # 发送登录请求
        request_data = {
            "action": "login",
            "username": username,
            "password": password
        }
        response = send_request(request_data)
        if response is None:
            show_error(self, "错误", "服务器无响应，请稍后重试")
            return

        if response.get("result") is True:
            # 登录成功
            # 简单判断管理员账号：admin/0000，其他为普通用户
            is_admin = (username == "admin")
            show_info(self, "成功", f"用户 {username} 登录成功")
            self.login_success.emit(username, is_admin)
        else:
            show_error(self, "失败", "用户名或密码错误")
    
    def register(self, username, password):
        # 发送注册请求
        request_data = {
            "action": "register",
            "username": username,
            "password": password
        }
        response = send_request(request_data)
        if response is None:
            show_error(self, "错误", "服务器无响应，请稍后重试")
            return
        
        if response.get("result") is True:
            show_info(self, "成功", "注册成功，请登录")
            self.on_switch_mode()
        else:
            show_error(self, "失败", "注册失败，用户名可能已存在")
